
using UnityEngine;

namespace WssUtils
{
    /// <summary>
    /// 单例的ScriptableObject，全局只会有一个 Scriptable 文件,
    /// 配置脚本会自动从Resources 路径 config/class.name 路径查找配置文件
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class SingletonScriptable<T> : ScriptableObject where T : ScriptableObject, new()
    {
        private static T m_instance = null;

        private static string GetPath()
        {
            return "config/" + typeof(T).Name;
        }


        public static T Instance
        {
            get
            {
                if (m_instance == null)
                {
                    T obj = null;

                    string path = GetPath();

                    //从编辑器配置读取
                    if (path != "")
                    {
#if TAP_TAP
                        obj = Resources.Load<T>(path + "_TapTap");
#endif

#if UNITY_ANDROID
                        if (obj == null) obj = Resources.Load<T>(path + "_Android");
#endif

#if UNITY_IOS
                        if (obj == null) obj = Resources.Load<T>(path + "_IOS");
#endif
                        //如果路径不对，则使用默认路径读取值
                        if (obj == null) obj = Resources.Load<T>(path);
                    }

                    if (obj != null)
                    {
                        m_instance = obj;
#if UNITY_EDITOR
                        Debug.Log("Load Res Data From: " + path);
#endif
                    }
                    else
                    {
                        //如果找不到配置，那么使用配置中的默认值
                        m_instance = ScriptableObject.CreateInstance<T>();
#if UNITY_EDITOR
                        Debug.Log("Load Res Data Default");
#endif
                    }
                }

                return m_instance;
            }
        }

        public void Load()
        {
            //空载方法，用于触发生成
        }


        public static void Destroy()
        {
            m_instance = null;
        }
    }



}

